from sqlalchemy import select

from poker_bot.core.interfaces import DiscountReader
from poker_bot.core.models.dto import DiscountDTO
from poker_bot.database.converters.discount import convert_db_model_to_discount_dto
from poker_bot.database.exception_mapper import exception_mapper
from poker_bot.database.models import Discount
from poker_bot.database.repositories.base import SQLAlchemyRepo


class DiscountReaderImpl(SQLAlchemyRepo, DiscountReader):
    async def get_discount_by_id(self, discount_id: int) -> DiscountDTO | None:
        query = select(Discount).where(Discount.id == discount_id)
        discount: Discount | None = await self._session.scalar(query)

        if not discount:
            return None

        return convert_db_model_to_discount_dto(discount)

    @exception_mapper
    async def get_discounts(self) -> list[DiscountDTO]:
        query = select(Discount)

        res = await self._session.scalars(query)
        discounts: list[Discount] = list(res)

        return [convert_db_model_to_discount_dto(discount) for discount in discounts]
